package com.nicho.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.nicho.pojo.Course;
import com.nicho.pojo.Student;
import com.nicho.pojo.Teacher;
import com.nicho.util.CloseStream;
import com.nicho.util.MD5;

/*
 * @Author: 姜东浩
 * @Date: 2021-11-11 23:22:09
 * @LastEditors: 姜东浩
 * @LastEditTime: 2021-11-11 23:22:09
 * @Description: 教师数据库操作
 */
public class TeacherDAO {

    static Teacher tea = null;
    ArrayList<Student> stuList = null;
    ArrayList<Teacher> teacherList = null;
    ArrayList<Course> courseList = null;
    static File studentData = new File("src/com/nicho/file/StudentData.txt");
    static File courseData = new File("src/com/nicho/file/CourseData.txt");
    static File teacherData = new File("src/com/nicho/file/TeacherData.txt");

    /**
     * @description: 教师登录验证
     * @param {Teacher} teacher
     * @return 数据库中的教师对象
     * @author: 姜东浩
     */
    public Teacher teacherLogin(Teacher teacher) {

        ObjectInputStream ois = null;
        // 如果存教师的文件不为空，则反序列化，否则返回null
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                teacherList = (ArrayList<Teacher>) ois.readObject();
            } else {
                System.out.println("文件为空");
                return null;
            }
            // 遍历教师列表，找到这个教师账号对应的教师，退出循环
            for (int i = 0; i < teacherList.size(); i++) {
                if (teacherList.get(i).getTeacherId().equals(teacher.getTeacherId())) {
                    tea = teacherList.get(i);
                    break;
                }
            }
            // 如果tea是null直接返回null
            if (tea == null) {
                return null;
            }
            // 如果传进来的账号密码与文件中保存的账号密码相等，则返回教师对象，否则返回null
            if (teacher.getTeacherId().equals(tea.getTeacherId())
                    && MD5.md5(teacher.getPassword()).equals(tea.getPassword())) {
                return tea;
            } else {
                tea = null;
                return null;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 修改自己的密码
     * @param {String} newPassword
     * @return 修改成功返回1，修改失败或有异常返回-1
     * @author: 姜东浩
     */
    public int updatePassword(String newPassword) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给teacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                teacherList = (ArrayList<Teacher>) ois.readObject();
            } else {
                return -1;
            }
            // 遍教师生列表，找到后修改密码，再把更新完的List序列化到数据库，返回1，没找到返回-1
            for (int i = 0; i < teacherList.size(); i++) {
                if (teacherList.get(i).getTeacherId().equals(tea.getTeacherId())) {
                    teacherList.get(i).setPassword(newPassword);
                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                    oos.reset();
                    oos.writeObject(teacherList);
                    return 1;
                }
            }
            return -1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: 查看自己所教课程的编号
     * @return 自己所教课程的编号，每找到或者抛出异常返回null
     * @author: 姜东浩
     */
    public String catMyTeachCourseId() {

        ObjectInputStream ois = null;
        // 如果存教师的文件不为空，则反序列化后传给teacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                teacherList = (ArrayList<Teacher>) ois.readObject();
            } else {
                return null;
            }
            // 遍教师生列表，返回找到自己所教课程的ID
            for (int i = 0; i < teacherList.size(); i++) {
                if (teacherList.get(i).getTeacherId().equals(tea.getTeacherId())) {
                    return teacherList.get(i).gettCourseId();
                }
            }
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 通过课程编号获取课程对象
     * @param {String} courseId
     * @return 找到后返回课程对象，失败返回null
     * @author: 姜东浩
     */
    public Course getCourseById(String courseId) {

        ObjectInputStream ois = null;
        // 如果存课程的文件不为空，则反序列化后传给courseList，否则返回null
        try {
            if (courseData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(courseData));
                courseList = (ArrayList<Course>) ois.readObject();
            } else {
                return null;
            }
            // 遍教课程列表，通过课程编号找对应课程，找到后返回课程对象
            for (int i = 0; i < courseList.size(); i++) {
                if (courseList.get(i).getCourseId().equals(courseId)) {
                    return courseList.get(i);
                }
            }
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 获取此教师管理的学生集合
     * @return 管理的学生集合或null
     * @author: 姜东浩
     */
    public ArrayList<Student> getMyStudentList() {

        ObjectInputStream ois = null;
        // 如果存学生的文件不为空，则反序列化后传给stuList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                teacherList = (ArrayList<Teacher>) ois.readObject();
            } else {
                return null;
            }
            // 遍历学生列表，找到后返回此学生所选课程的List
            for (int i = 0; i < teacherList.size(); i++) {
                if (teacherList.get(i).getTeacherId().equals(tea.getTeacherId())) {
                    return teacherList.get(i).getStudentList();
                }
            }
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: （全部打印）打印此教师管理的学生集合
     * @param {ArrayList<Student>} myStudentList
     * @author: 姜东浩
     */
    public void printMyStudentListAM(ArrayList<Student> myStudentList) {

        myStudentList.forEach(System.out::println);

    }

    /**
     * @description: （简单打印）打印此教师管理的学生集合
     * @param {ArrayList<Student>} myStudentList
     * @author: 姜东浩
     */
    public void printMyStudentList(ArrayList<Student> myStudentList) {

        for (int i = 0; i < myStudentList.size(); i++) {
            Student s = existStudentById(myStudentList.get(i).getStudentId());
            System.out.println(s.getStudentName());
            for (int j = 0; j < s.getsCourseList().size(); j++) {
                if (s.getsCourseList().get(j).getCourseId().equals(tea.gettCourseId())) {
                    System.out.println("学生学号：" + s.getStudentId() + "\t" + "学生姓名：" + s.getStudentName() + "\t" + "学生分数："
                            + s.getsCourseList().get(j).getGrade());
                }
            }
        }

    }

    /**
     * @description: 通过学号查找此学生是不是自己教的学生
     * @param {String} studentId
     * @return 在自己的学生集合中返回学生对象，不在返回null
     * @author: 姜东浩
     */
    public Student existStudentById(String studentId) {

        ObjectInputStream ois = null;
        // 如果存学生的文件不为空，则反序列化后传给stuList，否则返回null
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                stuList = (ArrayList<Student>) ois.readObject();
            } else {
                return null;
            }
            // 遍历List，找到后返回对象，否则返回null
            for (int i = 0; i < stuList.size(); i++) {
                if (stuList.get(i).getStudentId().equals(studentId)) {
                    return stuList.get(i);
                }
            }
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseStream.closeObjectInputStream(ois);
        }

    }

    /**
     * @description: 在学生数据库中设置学生分数
     * @param {String} courseId
     * @param {String} studentId
     * @param {Double} grade
     * @return success：1，error:-1
     * @author: 姜东浩
     */
    public int setCourseToStuInSL(String studentId, Double grade) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存学生的文件不为空，则反序列化后传给stuList，否则返回-1
        try {
            if (studentData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(studentData));
                stuList = (ArrayList<Student>) ois.readObject();
            } else {
                return -1;
            }
            // 遍历学生列表，找到后添加课程，再把更新完的List序列化到数据库，返回1，没找到返回-1
            for (int i = 0; i < stuList.size(); i++) {
                if (stuList.get(i).getStudentId().equals(studentId)) {
                    for (int j = 0; j < stuList.get(i).getsCourseList().size(); j++) {
                        if (stuList.get(i).getsCourseList().get(j).getCourseId().equals(tea.gettCourseId())) {
                            stuList.get(i).getsCourseList().get(j).setGrade(grade);
                            oos = new ObjectOutputStream(new FileOutputStream(studentData));
                            oos.reset();
                            oos.writeObject(stuList);
                            return 1;
                        }
                    }
                }
            }
            return -1;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

    /**
     * @description: (！！！没有使用这个方法)在教师数据库中更新自己管理的学生的分数
     * @param {String} courseId
     * @param {String} studentId
     * @param {Double} grade
     * @return success:1,throw error:-3,teacherData is null:-1,not found
     *         teacher、student、course:-2
     * @author: 姜东浩
     */
    public int setCourseToStuInTSL(String studentId, Double grade) {

        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        // 如果存教师的文件不为空，则反序列化后传给teacherList，否则返回-1
        try {
            if (teacherData.length() > 0) {
                ois = new ObjectInputStream(new FileInputStream(teacherData));
                teacherList = (ArrayList<Teacher>) ois.readObject();
            } else {
                return -1;
            }
            for (int i = 0; i < teacherList.size(); i++) {
                if (teacherList.get(i).getTeacherId().equals(tea.getTeacherId())) {
                    for (int j = 0; j < teacherList.get(i).getStudentList().size(); j++) {
                        if (teacherList.get(i).getStudentList().get(j).getStudentId().equals(studentId)) {
                            for (int k = 0; k < teacherList.get(i).getStudentList().get(j).getsCourseList()
                                    .size(); k++) {
                                if (teacherList.get(i).getStudentList().get(j).getsCourseList().get(k).getCourseId()
                                        .equals(tea.gettCourseId())) {
                                    teacherList.get(i).getStudentList().get(j).getsCourseList().get(k).setGrade(grade);
                                    oos = new ObjectOutputStream(new FileOutputStream(teacherData));
                                    oos.reset();
                                    oos.writeObject(teacherList);
                                    return 1;
                                }
                            }
                        }
                    }
                }
            }
            return -2;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return -3;
        } catch (IOException e) {
            e.printStackTrace();
            return -3;
        } finally {
            CloseStream.closeObjectStream(ois, oos);
        }

    }

}